package com.crm.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.util.ApplicationContext;

public class DBBackupDAO {
	public  void backup() throws Exception{
		byte[] data = dbBackUp().getBytes();		
		File filedst = new File("D:/backup.sql");
		//System.out.println("Sql Data Is: "+data);
		FileOutputStream dest = null;
		try {
			dest = new FileOutputStream(filedst);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			dest.write(data);
			dest.close();
			System.out.println("Sql Data Is: "+dest.toString());
			System.out.println("BackUp Complete");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public  String dbBackUp() {
		int BUFFER = 10485760;     
			  
			  Process run = null;
			try {
				run = Runtime.getRuntime().exec(
				           "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump --host=localhost --port=33006 --user=root --password=root" +
				           " --compact --databases --add-drop-table --complete-insert --extended-insert " +
				           "--skip-comments --skip-triggers kkcircuits");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			InputStream in = run.getInputStream();
			System.out.println("Input Stream Reader:"+in);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			System.out.println("Buffered Reader:"+br);
			StringBuffer temp = new StringBuffer();
			int count = 0;
			 char[] cbuf = new char[BUFFER];
			// System.out.println("Buffer Values Are"+cbuf.length);
			try {
				while ((count = br.read(cbuf, 0, BUFFER)) != -1)
				       	 temp.append(cbuf, 0, count);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("Count Value Is:"+count);
			System.out.println("Temp Value Is:"+temp);
			 try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			 try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			 return temp.toString();
			}
	
	
	public  void restore(){
		int processComplete = 0;
	    Process runtimeProcess = null;
	    String[] executeCmd = new String[]{
	    		"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysql kkcircuits --user=root --password=root -e D:\\kkcircuits.sql" };
	    try {
	    	runtimeProcess = Runtime.getRuntime().exec(executeCmd);
	    }catch (IOException ex){
	        ex.printStackTrace();
	    }
	    try{
	    	processComplete = runtimeProcess.waitFor();
	    }catch (InterruptedException ex){
	    	ex.printStackTrace();
	    }

	    if(processComplete == 0){
	    	javax.swing.JOptionPane.showMessageDialog(null, "Successfull");
	    }
	    else{
	    	javax.swing.JOptionPane.showMessageDialog(null, "","Un - Successfull",
	    			javax.swing.JOptionPane.ERROR_MESSAGE);
	    }

	}

}


